/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans;

import java.sql.Timestamp;
import java.io.Serializable;
import Utilities.Constants;
import Utilities.Interfaces.SelfValidating;

/**
 * This class is used for storing log data when writing it to or retrieving it
 * from the database.
 *
 * @author Angel Sanadinov
 */
public class LogBean implements Serializable, SelfValidating
{
    private int initiatorId;    //initiator of the event
    private int serverId;       //the id of the server on which the event took place
    private int eventId;        //id of the event *
    private String severity;    //event severity
    private String description; //event description
    private Timestamp eventDate;//event timestamp *
    //* only available when object is built with data from the database

    /**
     * Constructs a new object, most often used when log data is to be put in the database.
     *
     * @param initiatorId the initiator of the event
     * @param serverId the id of the server on which the event took place (set to 0
     *                 if the server id is not available)
     * @param eventSeverity the severity of the event
     * @param eventTime the timestamp of the event (in milliseconds)
     * @param eventDescription any associated description
     */
    public LogBean(int initiatorId, int serverId, String eventSeverity, long eventTime, String eventDescription)
    {
        this.initiatorId = initiatorId;
        this.serverId = serverId;
        this.severity = eventSeverity;
        this.eventDate = new Timestamp(eventTime);
        this.description = eventDescription;
        this.eventId = Constants.INVALID_EVENT_ID;
    }

    /**
     * Constructs a new object, most often used when log data is retrieved from the database.
     *
     * @param initiatorId the initiator of the event
     * @param serverId the id of the server on which the event took place (set to 0
     *                 if the server id is not available)
     * @param eventId the id of the event
     * @param eventSeverity the severity of the event
     * @param eventTime the timestamp of the event
     * @param eventDescription any associated description
     */
    public LogBean(int initiatorId, int serverId, int eventId, String eventSeverity, Timestamp eventTime, String eventDescription)
    {
        this.initiatorId = initiatorId;
        this.serverId = serverId;
        this.severity = eventSeverity;
        this.eventDate = eventTime;
        this.description = eventDescription;
        this.eventId = eventId;
    }

    /**
     * Retrieves the initiator of the event.
     *
     * @return the id of the event initiator
     */
    public int getInitiatorId()
    {
        return initiatorId;
    }

    /**
     * Retrieves the id of the server on which the event took place.
     *
     * @return the id of the server or 0 if no id is available
     */
    public int getServerId()
    {
        return serverId;
    }

    /**
     * Retrieves the event id, if available.
     *
     * @return the id of the event
     */
    public int getEventId()
    {
        return eventId;
    }

    /**
     * Retrieves the severity of the event.
     *
     * @return the event severity
     */
    public String getSeverity()
    {
        return severity;
    }

    /**
     * Retrieves the description of the event.
     *
     * @return the event description
     */
    public String getDescription()
    {
        return description;
    }

    /**
     * Retrieves a copy of the time of the event.
     *
     * @return event timestamp
     */
    public Timestamp getEventDate()
    {
        return (Timestamp)eventDate.clone();
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * <b>Note:</b> <i>For object of class <code>LogBean</code> this method
     *              always returns <code>true</code>. The validity of this object
     *              is irrelevant as there is no action that can be taken to solve
     *              potential issues.</i>
     *
     * @return <code>true</code>
     */
    @Override
    public boolean isValid()
    {
        return true;
    }

    /**
     * Builds a CSV list of the data stored in the object. <br><br>
     *
     * Format: <i>eventId,initiatorId,serverId,severity,eventDate,description</i>
     *
     * @return the <code>String</code> representation of the object
     */
    @Override
    public String toString()
    {
        return eventId + "," + initiatorId + "," + serverId + "," + severity + "," + eventDate.toString() + "," + description;
    }
}
